/**
 * 
 */
package com.jie.eclipse.pan.generator;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.jie.eclipse.pan.config.BaseConfigHelper;
import com.jie.eclipse.pan.config.BasePageConfig;
import com.jie.eclipse.pan.config.toolbar.ToolbarConfig;
import com.jie.eclipse.pan.data.BaseDataEntity;
import com.jie.eclipse.pan.popup.wizard.BaseWizard;
import com.jie.eclipse.pan.utils.Utils;

/**
 * @author yanjie
 *
 */
public class BaseGenertor {
	protected BaseConfigHelper configHelper = null;
	protected BaseWizard baseWizard = null;
	
	public BaseWizard getBaseWizard() {
		return baseWizard;
	}
	public void setBaseWizard(BaseWizard baseWizard) {
		this.baseWizard = baseWizard;
	}
	/**
	 * 
	 */
	public BaseGenertor() {
		this.configHelper = new BaseConfigHelper();
	}
	public ByteArrayInputStream GeneratorUIJspCode(BasePageConfig pageConfig) throws UnsupportedEncodingException{
		StringBuffer code = null;
		if(BasePageConfig.TAG_TPTYPE_BASE_GRID.equals(pageConfig.getTpType())){
			code = GeneratorGridUIJspCode(pageConfig);
		}else if(BasePageConfig.TAG_TPTYPE_BASE_FORM.equals(pageConfig.getTpType())){
			code = GeneratorFormUIJspCode(pageConfig);
		}else if(BasePageConfig.TAG_TPTYPE_BASE_LOADING.equals(pageConfig.getTpType())){
			code = GeneratorCodeByTp(getParamsByPageConfig(pageConfig), "base/loading/loading_ui.resource");
		}else if(BasePageConfig.TAG_TPTYPE_BASE_PRINT.equals(pageConfig.getTpType())){
			code = GeneratorCodeByTp(getParamsByPageConfig(pageConfig), "base/print/print_ui.resource");
		}else if(BasePageConfig.TAG_TPTYPE_BASE_REPORT.equals(pageConfig.getTpType())){
			code = GeneratorCodeByTp(getParamsByPageConfig(pageConfig), "base/report/report_ui.resource");
		}
		return new ByteArrayInputStream(code == null ? "".getBytes("UTF-8") : code.toString().getBytes("UTF-8"));
	}
	
	public ByteArrayInputStream GeneratorBackEndJspCode(BasePageConfig pageConfig) throws UnsupportedEncodingException{
		StringBuffer code = new StringBuffer();
		if(BasePageConfig.TAG_TPTYPE_BASE_GRID.equals(pageConfig.getTpType())){
			code = GeneratorGridBackEndJspCode(pageConfig);
		}else if(BasePageConfig.TAG_TPTYPE_BASE_FORM.equals(pageConfig.getTpType())){
			code = GeneratorFormBackEndJspCode(pageConfig);
		}else if(BasePageConfig.TAG_TPTYPE_BASE_LOADING.equals(pageConfig.getTpType())){
			code = GeneratorCodeByTp(getParamsByPageConfig(pageConfig), "base/loading/loading_backend.resource");
		}else if(BasePageConfig.TAG_TPTYPE_BASE_PRINT.equals(pageConfig.getTpType())){
			code = GeneratorCodeByTp(getParamsByPageConfig(pageConfig), "base/print/print_backend.resource");
		}else if(BasePageConfig.TAG_TPTYPE_BASE_REPORT.equals(pageConfig.getTpType())){
			code = GeneratorCodeByTp(getParamsByPageConfig(pageConfig), "base/report/report_backend.resource");
		}
		return new ByteArrayInputStream(code.toString().getBytes("UTF-8"));
	}
	
	/**
	 * @param pageConfig
	 * @param type
	 * @return
	 * @throws UnsupportedEncodingException
	 * 根据模板路径构建代码
	 */
	public ByteArrayInputStream GeneratorCrud1Code(BasePageConfig pageConfig,String type)throws UnsupportedEncodingException{
		StringBuffer code = null;
		if("main".equals(type)){
			code = GeneratorCodeByTp(pageConfig.getDataRecord(),"grid/grid2/crud1.resource");
		}else if("left".equals(type)){
			code = GeneratorCodeByTp(pageConfig.getDataRecord(),"grid/grid2/crud1_left.resource");
		}else if("leftbackend".equals(type)){
			code = GeneratorCodeByTp(pageConfig.getDataRecord(),"grid/grid2/crud1_left_backend.resource");
		}else if("right".equals(type)){
			code = GeneratorCodeByTp(pageConfig.getDataRecord(),"grid/grid2/crud1_right.resource");
		}else if("rightbackend".equals(type)){
			code = GeneratorCodeByTp(pageConfig.getDataRecord(),"grid/grid2/crud1_right_backend.resource");
		}
		return new ByteArrayInputStream(code == null ? "".getBytes("UTF-8") : code.toString().getBytes("UTF-8"));
	}
	
	public ByteArrayInputStream GeneratorInputStreamByTp(BaseDataEntity data,String path)throws UnsupportedEncodingException{
		StringBuffer code = GeneratorCodeByTp(data,path);
		return new ByteArrayInputStream(code == null ? "".getBytes("UTF-8") : code.toString().getBytes("UTF-8"));
	}
	
	public StringBuffer GeneratorCodeByTp(BaseDataEntity data,String path){
		final String newline = System.getProperty("line.separator");
		String line;
		StringBuffer sb = new StringBuffer();
		try {
			InputStream input = this.getClass().getResourceAsStream(
					"tp/"+path);
			BufferedReader reader = new BufferedReader(new InputStreamReader(input,"UTF-8"));
			try {
				while ((line = reader.readLine()) != null) {
					line = processFormat(data,line);
					sb.append(line);
					sb.append(newline);
				}

			} finally {
				reader.close();
			}

		} catch (IOException e) {
			Utils.print(e.getMessage());
		}
		return sb;
	}
	
	public String processFormat(BaseDataEntity data,String line){
		Pattern   pat   =   Pattern.compile("%%\\{[^\\}]*\\}%%");
		Matcher   mat   =pat.matcher(line);
		while(mat.find()){
			String strParam = mat.group();
			strParam = strParam.substring(3, strParam.length()-3);
			line = mat.replaceFirst(data.gpsv(strParam));
			mat = pat.matcher(line);
		}
		return line;
	}
	
	/**
	 * @return
	 * 返回表格UI code
	 */
	public StringBuffer GeneratorGridUIJspCode(BasePageConfig pageConfig){
		return null;
	}
	
	
	
	/**
	 * @return
	 * 返回表單 UI code
	 */
	public StringBuffer GeneratorFormUIJspCode(BasePageConfig pageConfig){
		return null;
	}
	
	
	
	
	
	/**
	 * @return
	 * 返回表格BackEnd code
	 */
	public StringBuffer GeneratorGridBackEndJspCode(BasePageConfig pageConfig){
		return null;
	}
	
	/**
	 * @return
	 * 返回表單 BackEnd code
	 */
	public StringBuffer GeneratorFormBackEndJspCode(BasePageConfig pageConfig){
		return null;
	}
	
	public ByteArrayInputStream GeneratorConfigXmlCode(BasePageConfig pageConfig) throws UnsupportedEncodingException{
		StringBuffer code = new StringBuffer();
		if(BasePageConfig.TAG_TPTYPE_BASE_GRID.equals(pageConfig.getTpType())){
			code = GeneratorGridConfigXmlCode(pageConfig);
		}else if(BasePageConfig.TAG_TPTYPE_BASE_FORM.equals(pageConfig.getTpType())){
			code = GeneratorFormConfigXmlCode(pageConfig);
		}
		return new ByteArrayInputStream(code.toString().getBytes("UTF-8"));
	}
	
	/**
	 * @return
	 * 返回表格配置Xml code
	 */
	public StringBuffer GeneratorGridConfigXmlCode(BasePageConfig pageConfig){
		return null;
	}
	
	/**
	 * @return
	 * 返回表單配置 Xml code
	 */
	public StringBuffer GeneratorFormConfigXmlCode(BasePageConfig pageConfig){
		return null;
	}
	
	public String GetToolbarCode(BasePageConfig pageConfig){
		ToolbarConfig defaultConfig = new ToolbarConfig();
		defaultConfig.loadConfig(getDefaultToolbar(pageConfig.getTpType()));
		
		ToolbarConfig tbConfig = pageConfig.getTbConfig();
		if(tbConfig == null){
			tbConfig = (ToolbarConfig)defaultConfig.clone();
		}
		
		String[] actions = {"new","search","save","cancel","delete","copy","start","check","export","print","help"};
		StringBuffer toolbar = new StringBuffer();
		toolbar.append("<jm:toolbar");
		for (int i = 0; i < actions.length; i++) {
			String action = actions[i];
			if(!tbConfig.gpov(action+"Disabled").equals(defaultConfig.gpov(action+"Disabled"))){
				toolbar.append(" "+action+"Disabled=\""+String.valueOf(!tbConfig.gpbv(action+"Disabled",false))+"\"");
			}
			
			if(!tbConfig.gpov(action+"Confirm").equals(defaultConfig.gpov(action+"Confirm"))){
				toolbar.append(" "+action+"Confirm=\""+tbConfig.gpsv(action+"Confirm")+"\"");
			}
			
			if(!tbConfig.gpov(action+"Enab").equals(defaultConfig.gpov(action+"Enab"))){
				toolbar.append(" "+action+"Enab=\""+tbConfig.gpsv(action+"Enab")+"\"");
			}
		}
		toolbar.append(" />");
		return toolbar.toString();
	}
	
	public BaseDataEntity getDefaultToolbar(String tpType){
		BaseDataEntity config = new BaseDataEntity();
		if(BasePageConfig.TAG_TPTYPE_BASE_GRID.equals(tpType)){
			config.spbv("newDisabled", false);
			config.spbv("newConfirm", false);
			config.spbv("newEnab", true);
			
			config.spbv("searchDisabled", true);
			config.spbv("searchConfirm", false);
			config.spbv("searchEnab", true);
			
			config.spbv("saveDisabled", false);
			config.spbv("saveConfirm", false);
			config.spbv("saveEnab", true);
			
			
			config.spbv("cancelDisabled", false);
			config.spbv("cancelConfirm", false);
			config.spbv("cancelEnab", true);
			
			config.spbv("deleteDisabled", false);
			config.spbv("deleteConfirm", true);
			config.spbv("deleteEnab", true);
			
			config.spbv("copyDisabled", false);
			config.spbv("copyConfirm", false);
			config.spbv("copyEnab", true);
			
			config.spbv("startDisabled", false);
			config.spbv("startConfirm", false);
			config.spbv("startEnab", true);
			
			config.spbv("checkDisabled", false);
			config.spbv("checkConfirm", false);
			config.spbv("checkEnab", true);
			
			config.spbv("exportDisabled", true);
			config.spbv("exportConfirm", false);
			config.spbv("exportEnab", true);
			
			config.spbv("printDisabled", true);
			config.spbv("printConfirm", false);
			config.spbv("printEnab", true);
			
			config.spbv("helpDisabled", false);
			config.spbv("helpConfirm", false);
			config.spbv("helpEnab", true);
		}else if(BasePageConfig.TAG_TPTYPE_BASE_FORM.equals(tpType)){
			config.spbv("newDisabled", false);
			config.spbv("newConfirm", false);
			config.spbv("newEnab", true);
			
			config.spbv("searchDisabled", false);
			config.spbv("searchConfirm", false);
			config.spbv("searchEnab", true);
			
			config.spbv("saveDisabled", true);
			config.spbv("saveConfirm", false);
			config.spbv("saveEnab", true);
			
			
			config.spbv("cancelDisabled", false);
			config.spbv("cancelConfirm", false);
			config.spbv("cancelEnab", true);
			
			config.spbv("deleteDisabled", true);
			config.spbv("deleteConfirm", true);
			config.spbv("deleteEnab", true);
			
			config.spbv("copyDisabled", false);
			config.spbv("copyConfirm", false);
			config.spbv("copyEnab", true);
			
			config.spbv("startDisabled", false);
			config.spbv("startConfirm", false);
			config.spbv("startEnab", true);
			
			config.spbv("checkDisabled", false);
			config.spbv("checkConfirm", false);
			config.spbv("checkEnab", true);
			
			config.spbv("exportDisabled", true);
			config.spbv("exportConfirm", false);
			config.spbv("exportEnab", true);
			
			config.spbv("printDisabled", true);
			config.spbv("printConfirm", false);
			config.spbv("printEnab", true);
			
			config.spbv("helpDisabled", false);
			config.spbv("helpConfirm", false);
			config.spbv("helpEnab", true);
		}
		return config;
	}
	
	public BaseConfigHelper getConfigHelper() {
		return configHelper;
	}
	
	public BaseDataEntity getParamsByPageConfig(BasePageConfig pageConfig){
		BaseDataEntity params = new BaseDataEntity();
		params.spsv("pageTitle", pageConfig.getPageTitle());
		params.spsv("pageName", pageConfig.getPageName());
		return params;
	}
	
	
}
